package com.police.activity;


import org.xutils.common.Callback.Cancelable;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;

import com.police.base.BaseActivity;
import com.police.bean.response.CardResponse;
import com.police.bean.response.MovieResponse;
import com.police.http.HttpApi;
import com.police.http.PsCallBack;
import com.policestone.R;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

@ContentView(R.layout.test_http_api_activity)
public class HttpApiTestActivity extends BaseActivity
{
    private static final String TAG = "MainActivity";
    private Cancelable getJsonApi;
    private Cancelable getJsonListApi;
    private Cancelable getXmlApi;

	@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

    }
   
	
	@Event(value = { R.id.button_cancel_get_json, 
					 R.id.button_cancel_get_xml,
					 R.id.button_cancel_post_json, 
					 R.id.button_cancel_post_xml })
	private void getCancelEvent(View view)
	{
		switch (view.getId())
		{
		case R.id.button_cancel_get_json:
			getJsonApi.cancel();	
			break;

		case R.id.button_cancel_post_json:

			break;

		case R.id.button_cancel_get_xml:

			break;
		case R.id.button_cancel_post_xml:

			break;
			
		default:
			break;
		}
	}
	
	
	
	
	/**
	 * @Title: getEvent   
	 * @Description: 发送请求的按钮点击处理
	 * @param: @param view      
	 * @return: void      
	 * @throws
	 */
    @Event(value={R.id.button_get_json, 
    		      R.id.button_get_json_list,
    		      R.id.button_get_xml, 
    		      R.id.button_post_xml})
    private void getEvent(View view)
    {
    	showHud();
		switch(view.getId())
		{
		case R.id.button_get_json:
			getJsonTest();
			break;
			
		case R.id.button_get_json_list:
			getJsonListTest();
			break;
			
		case R.id.button_get_xml:
			getXmlTest();
			break;
			
		case R.id.button_post_xml:
			postXmlTest();
			break;
			
		}
    }
    
    private void getJsonTest()
    {
    	Log.i(TAG, "getJsonTest()");
    	
    	getJsonApi = HttpApi.getCardInfoTest("450311198003013014",
				new PsCallBack<CardResponse>()
				{
					@Override
					public void onSuccess(CardResponse result)
					{
						super.onSuccess(result);
					}

					@Override
					public void onError(Throwable ex, boolean isOnCallback)
					{
						super.onError(ex, isOnCallback);
					}

					@Override
					public void onCancelled(CancelledException cex)
					{
						super.onCancelled(cex);
					}

					@Override
					public void onFinished()
					{
						super.onFinished();
						// 请求结束后，取消进度条
						dismissHud();
					}

				});

    }
    
    private void getJsonListTest()
 {
		Log.i(TAG, "getJsonListTest()");
		getJsonListApi = HttpApi.getMoivesTest("US",
				new PsCallBack<MovieResponse>() 
				{

					@Override
					public void onSuccess(MovieResponse result) {
						super.onSuccess(result);

					}

					@Override
					public void onError(Throwable ex, boolean isOnCallback) {
						super.onError(ex, isOnCallback);
					}

					@Override
					public void onCancelled(CancelledException cex) {
						super.onCancelled(cex);
					}

					@Override
					public void onFinished() {
						super.onFinished();
						// 请求结束后，取消进度条
						dismissHud();
					}

				});
	}
    
    
    
    
    /**
     * @Title: getXmlTest   
     * @Description: 请求返回xml格式测试 
     * @param:       
     * @return: void      
     * @throws
     */
    private void getXmlTest()
    {
    	Log.i(TAG, "postJsonTest()");
		getXmlApi = HttpApi.getCardInfoTestXml("450311198003013014",
				new PsCallBack<CardResponse>() {

				});

    }
    
    private void postXmlTest()
    {
    	Log.i(TAG, "postXmlTest()");
    }
    
    
	
	/* 
	 * 如果网络请求时间较长，进度条一直在转
	 * 点击取消键可以直接取消网络请求
	 */
	@Override
	protected void canceledHud() 
	{
		super.canceledHud();
		
		if(getJsonApi != null)
		{
			if(getJsonApi.isCancelled() == false)
			{
				getJsonApi.cancel();
				Log.i(TAG, "getJsonApi canceled");
			}
		}
		
		if(getXmlApi != null)
		{
			if(getXmlApi.isCancelled() == false)
			{
				getXmlApi.cancel();
				Log.i(TAG, "getXmlApi canceled");
			}
		}
		
		if(getJsonListApi != null)
		{
			if(getJsonListApi.isCancelled() == false)
			{
				getJsonListApi.cancel();
				Log.i(TAG, "getJsonListApi canceled");
			}
		}
		
	}
	
    
}
